<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Exception;//事务处理
use app\admin\model\Promodel;

class Buyterms extends Auth
{
    public function __construct(Request $request){
        parent::__construct($request);
        $this->data = $request->param();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
    }

   
    // 合同条款
    public function list(Request $request)
    {
        if($request->isAjax()){
            $listdata = getDatas('buy_terms',['*'],'1=1','drank asc');
            $returnData['size'] = count( $listdata);
            if(isset($this->data['page']) && isset($this->data['limit'])){
                $listdata = array_slice($listdata,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
                $returnData['page'] = $this->data['page'];
            }
            foreach ($listdata as $k => $v) {
                $listdata[$k]['create_name'] = $v['create_id'] == '' ? '-' : getinfo($v['create_id'], 'admin')['nickname'];//编辑者
                $listdata[$k]['created_time'] = $v['create_at'] == '' ? '-' : getTimeDates($v['create_at'],9,$this->lang);
                $listdata[$k]['term_name'] = lang($v['type']);
                $listdata[$k]['must'] = $v['is_must'] == 1 ? 'o' : '-';
            }
            // dump($terms);die;
            $returnData['listdata'] = $listdata;
            return json_encode($returnData);
        }else{
            return view('list');
        }
    }


    // add
    public function addTerms(){
        if(isset($this->data['request']) && $this->data['request'] == 'insert'){
            unset($this->data['request']);
            if(!isset($this->data['type']) || $this->data['type'] == '') return json_encode(array('status'=>2,'msg'=>lang("please_select_a_contract_term_type")));
            if(!isset($this->data['content']) || $this->data['content'] == '') return json_encode(array('status'=>2,'msg'=>lang("please_fill_in_the_terms_of_contract")));
            $this->data['is_must'] = $this->data['is_must'] == 'true' ? 1 : 0;
            $this->data['create_id'] = $this->user_id;
            $this->data['create_at'] = time();
            $res = insertData('buy_terms',$this->data);
            if(!$res) return json_encode(array('status'=>2,'msg'=>$this->data['type'].lang("contract_terms_failed_to_be_added")));
            return json_encode(array('status'=>1,'msg'=>$this->data['type'].' '.lang("contract_terms_added_successfully")));
        }else{
            return view('open',['title'=>lang("add_terms_of_purchase_contract"),'request'=>'add']);
        }
    }

    // edit
    public function editTerms(){

        if(!isset($this->data['id'])) return json_encode(array('status'=>2,'msg'=>lang("please_select_the_contract_terms_to_be_edited")));
        if(isset($this->data['request']) && $this->data['request'] == 'update'){
            unset($this->data['request']);
            $id = $this->data['id'];
            unset($this->data['id']);
            $res = updateData('buy_terms',['id'=>$id],$this->data);
            if(!$res) return json_encode(array('status'=>0,'msg'=>'No.'.$id.','.lang('edit_failed')));
            return json_encode(array('status'=>1,'msg'=>'No.'.$id.','.lang('edited_successfully')));

        }else{
            $terms = getDatas('buy_terms',['*'],['id'=>$this->data['id']],'id asc');
            if(!empty($terms)){
                $terms = $terms[0];
            }
            return view('open',['title'=>lang("edit_terms_of_purchase_contract"),'request'=>'edit','terms'=>$terms]);
        }
    }

    public function requestDel()
    {
        if(!isset($this->data['id'])) return json_encode(array('status'=>2,'msg'=>lang("delete_failed,_please_select_the_contract_terms_to_delete")));
        $res = deleteData('buy_terms',['id'=>$this->data['id']]);
        if($res) return json_encode(array('status'=>1,'msg'=>'No.'.$this->data['id'].','.lang('cleared_successfully')));
        return json_encode(array('status'=>0,'msg'=>'No.'.$this->data['id'].','.lang('clear_failed')));
    }

}